﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.IO;

namespace PloobsEngine.Loader
{
    /// <summary>
    /// Not yet Finished
    /// </summary>
    public class ModelExtractor : IModelLoader
    {

        public ModelLoaderData Load(string modelName)
        {
            ModelLoaderData elements = new ModelLoaderData();
           
            Model model = EngineStuff.CustomContentManager.GetAsset<Model>("..\\Content\\Model\\" + modelName);
            Matrix[] m = new Matrix[model.Bones.Count];
            model.CopyBoneTransformsTo(m);

            ////////////EXTRAINDO MESHES

            for (int i = 0; i < model.Meshes.Count; i++)
            {
                Matrix tr = Matrix.Identity;

                ModelBone mb = model.Meshes[i].ParentBone;
                while (mb != null)
                {
                    tr *= mb.Transform;
                    mb = mb.Parent;
                }

                Vector3 scale;
                Vector3 pos;
                Quaternion ori;
                tr.Decompose(out scale, out ori, out pos);

                String name = model.Meshes[i].Name.Substring(5);

                BasicEffect basiceffect = model.Meshes[i].Effects[0] as BasicEffect;
                Texture2D difuse = basiceffect.Texture;

                //String nome = name + "_normal.xnb";                

                //if (File.Exists("//Content//Textures//" + nome))
                //{

                //}


                ModelInformation mi = new ModelInformation();
                mi.difuse = difuse;                
                mi.glow = null;
                mi.specular = null;
                mi.normal = null;
                mi.ori = ori;
                mi.pos = pos;
                mi.scale = scale;
                mi.modelMesh = model.Meshes[i];

                elements.ModelMeshesInfo.Add(mi);
            }

            //BoundingBox bb = new BoundingBox();
            //foreach (var item in elements[OBJETOS])
            //{
            //    bb = BoundingBox.CreateMerged(bb, (item as IObject).PhysicObject.BoundingBox);
            //}
            //elements[GRIDBOUND].Add(bb);

            /////////////////////EXTRAINDO OQ NAO MESH

        //    for (int i = 0; i < model.Root.Children.Count; i++)
        //    {
        //        Matrix tr = Matrix.Identity;

        //        ModelBone mb = model.Root.Children[i];
        //        while (mb != null)
        //        {
        //            tr *= mb.Transform;
        //            mb = mb.Parent;
        //        }

        //        Vector3 scale;
        //        Vector3 pos;
        //        Quaternion ori;
        //        tr.Decompose(out scale, out ori, out pos);

        //        String name = model.Root.Children[i].Name;
        //        if (name.Contains("DUMMY"))
        //        {
        //            ModelInformation mi = new ModelInformation();
        //            mi.difuse = null;                    
        //            mi.glow = null;
        //            mi.specular = null;
        //            mi.normal = null;
        //            mi.ori = ori;
        //            mi.pos = pos;
        //            mi.scale = scale;
        //            mi.modelMesh = null;
        //            elements.ModelMeshesInfo.Add(mi);
        //        }
        //    }
            return elements;
        }
    }
}
